home *** CD-ROM | disk | FTP | other *** search
/ Info-Mac 4 / Info_Mac IV CD-ROM (Pacific HiTech Inc.)(August 1994).iso / Development / Source / WAREZ 1.1 source Folder / warez ƒ / MSG Shell ƒ / msg graphics.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-02-07  |  11.0 KB  |  438 lines  |  [TEXT/KAHL]

  1. /**********************************************************************\
  2.  
  3. File:        msg graphics.c
  4.  
  5. Purpose:    This module handles opening/closing/updating all windows:
  6.             help window and about windows.  This includes
  7.             manipulating offscreen bitmaps for fun and no profit.
  8.  
  9.  
  10. WAREZ -=- nostalgia isn't what it used to be
  11. Copyright ©1994, Mark Pilgrim
  12.  
  13. This program is free software; you can redistribute it and/or modify
  14. it under the terms of the GNU General Public License as published by
  15. the Free Software Foundation; either version 2 of the License, or
  16. (at your option) any later version.
  17.  
  18. This program is distributed in the hope that it will be useful,
  19. but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  21. GNU General Public License for more details.
  22.  
  23. You should have received a copy of the GNU General Public License
  24. along with this program in a file named "GNU General Public License".
  25. If not, write to the Free Software Foundation, 675 Mass Ave,
  26. Cambridge, MA 02139, USA.
  27.  
  28. \**********************************************************************/
  29.  
  30. #include "msg graphics.h"
  31. #include "msg about.h"
  32. #include "msg help.h"
  33. #include "msg dialogs.h"
  34. #include "msg error.h"
  35. #include "msg menus.h"
  36. #include "msg environment.h"
  37. #include "msg prefs.h"
  38. #include "program globals.h"
  39. #include "warez.h"
  40.  
  41. Boolean            gInitedWindowBounds[NUM_WINDOWS];
  42. Rect            gMainScreenBounds;
  43. Rect            gWindowBounds[NUM_WINDOWS];
  44. GDHandle        gBiggestDevice;
  45. WindowPtr        gTheWindow[NUM_WINDOWS];
  46. int                gWindowWidth[NUM_WINDOWS];
  47. int                gWindowHeight[NUM_WINDOWS];
  48. Str255            gWindowTitle[NUM_WINDOWS];
  49. int                gWindowType[NUM_WINDOWS];
  50. Boolean            gOffscreenNeedsUpdate[NUM_WINDOWS];
  51. int                gNumHelp;
  52.  
  53. /* internal stuff */
  54. Rect            bRect[NUM_WINDOWS];
  55. Ptr                myBits[NUM_WINDOWS];
  56. CGrafPort        myCGrafPort[NUM_WINDOWS];
  57. CGrafPtr        myCGrafPtr[NUM_WINDOWS];
  58. CTabHandle        ourCMHandle[NUM_WINDOWS];
  59. GrafPort        myGrafPort[NUM_WINDOWS];
  60. GrafPtr            myGrafPtr[NUM_WINDOWS];
  61. int                gLastDepth[NUM_WINDOWS];
  62. int                gMaxDepth[NUM_WINDOWS];
  63.  
  64. void InitMSGGraphics(void)
  65. {
  66.     int                i;
  67.         
  68.     gWindowWidth[kAbout]=200;
  69.     gWindowHeight[kAbout]=220;
  70.     
  71.     gWindowWidth[kAboutMSG]=243;
  72.     gWindowHeight[kAboutMSG]=243;
  73.     
  74.     gWindowWidth[kHelp]=300;
  75.     gWindowHeight[kHelp]=250;
  76.     
  77.     for (i=0; i<NUM_WINDOWS; i++)
  78.     {
  79.         myCGrafPtr[i]=myGrafPtr[i]=gTheWindow[i]=0L;
  80.         gInitedWindowBounds[i]=FALSE;
  81.         gOffscreenNeedsUpdate[i]=TRUE;
  82.     }
  83.     
  84.     gMaxDepth[kAbout]=8;
  85.     gMaxDepth[kAboutMSG]=1;
  86.     gMaxDepth[kHelp]=1;
  87.     
  88.     gWindowType[kAbout]=altDBoxProc;
  89.     gWindowType[kAboutMSG]=plainDBox;
  90.     gWindowType[kHelp]=noGrowDocProc;
  91.     
  92.     StuffHex(gWindowTitle[kHelp], "\p0448454c50");
  93.     gWindowTitle[kAbout][0]=gWindowTitle[kAboutMSG][0]==0x00;
  94. }
  95.  
  96. void OpenTheWindow(int index)
  97. {
  98.     unsigned long        dummy;
  99.     
  100.     if (!gTheWindow[index])
  101.     {
  102.         if (!gInitedWindowBounds[index])
  103.         {
  104.             if (index==kHelp)
  105.             {
  106.                 gWindowBounds[index].left=10;
  107.                 gWindowBounds[index].top=50;
  108.             }
  109.             else
  110.             {
  111.                 gWindowBounds[index].left = gMainScreenBounds.left + (((gMainScreenBounds.right -
  112.                             gMainScreenBounds.left) - gWindowWidth[index]) / 2);
  113.                 gWindowBounds[index].top = 9+ gMainScreenBounds.top + (((gMainScreenBounds.bottom -
  114.                             gMainScreenBounds.top) - gWindowHeight[index]) / 2);
  115.             }
  116.             if(gWindowBounds[index].top < 30)
  117.                 gWindowBounds[index].top = 30;
  118.             gWindowBounds[index].bottom = gWindowBounds[index].top + gWindowHeight[index];
  119.             gWindowBounds[index].right = gWindowBounds[index].left + gWindowWidth[index];
  120.             gInitedWindowBounds[index]=TRUE;
  121.         }
  122.         
  123.         if(gHasColorQD)
  124.         {
  125.             gTheWindow[index] = NewCWindow(0L, &gWindowBounds[index], gWindowTitle[index],
  126.                 TRUE, gWindowType[index], (WindowPtr)-1L, TRUE, 0L);
  127.         }
  128.         else
  129.         {
  130.             gTheWindow[index] = NewWindow(0L, &gWindowBounds[index], gWindowTitle[index],
  131.                 TRUE, gWindowType[index], (WindowPtr)-1L, TRUE, 0L);
  132.         }
  133.  
  134.         bRect[index] = gTheWindow[index]->portRect;
  135.     }
  136.     
  137.     if (gTheWindow[index])
  138.     {
  139.         SelectWindow(gTheWindow[index]);
  140.         SetPort(gTheWindow[index]);
  141.         UpdateTheWindow(index);
  142.         if (index==kAboutMSG)
  143.             Delay(30, &dummy);
  144.     }
  145.     else ErrorString("\pTHERE 15 N0T ENUF MEM0RY T0 0PEN THE W1ND0W!!", "\p");
  146. }
  147.  
  148. void GetMainScreenBounds(void)
  149. {
  150.     gMainScreenBounds = screenBits.bounds;
  151.     gMainScreenBounds.top += MBarHeight;
  152. }
  153.  
  154. int GetWindowDepth(int index)
  155. {
  156.     Rect        tempRect;
  157.     long        biggestSize;
  158.     long        tempSize;
  159.     GDHandle    thisHandle;
  160.     
  161.     if (gHasColorQD)
  162.     {
  163.         if (gTheWindow[index])
  164.         {
  165.             thisHandle = GetDeviceList();
  166.             gBiggestDevice = 0L;
  167.             biggestSize = 0L;
  168.             
  169.             while (thisHandle)
  170.             {
  171.                 if (TestDeviceAttribute(thisHandle, screenDevice) &&
  172.                             TestDeviceAttribute(thisHandle, screenActive))
  173.                     if (SectRect(&(gTheWindow[index]->portRect), &((**thisHandle).gdRect),
  174.                                 &tempRect))
  175.                         if (biggestSize < (tempSize =
  176.                                 ((long)(tempRect.bottom - tempRect.top))
  177.                                 * ((long)(tempRect.right - tempRect.left))))
  178.                         {
  179.                             biggestSize = tempSize;
  180.                             gBiggestDevice = thisHandle;
  181.                         }
  182.                 thisHandle = GetNextDevice(thisHandle);
  183.             }
  184.             
  185.             if (gBiggestDevice)
  186.                 return (**(**gBiggestDevice).gdPMap).pixelSize;
  187.             else
  188.                 return 1;
  189.         }
  190.         else
  191.         {
  192.             return (**(**GetMainDevice()).gdPMap).pixelSize;
  193.         }
  194.     }
  195.     else
  196.     {
  197.         return 1;
  198.     }
  199. }
  200.  
  201. void UpdateTheWindow(int index)
  202. {
  203.     long        offRowBytes, sizeOfOff;
  204.     int            theDepth, i, err;
  205.     GDHandle    oldDevice;
  206.  
  207.     if (((theDepth = GetWindowDepth(index)) > 2) && (gMaxDepth[index]>2))
  208.     {
  209.         /* if we just changed from one color depth to another color depth */
  210.         if((myCGrafPtr[index] != 0L) && (gLastDepth[index] != theDepth))
  211.         {
  212.             DisposeHandle((**(myCGrafPort[index]).portPixMap).pmTable);
  213.             DisposePtr((**(myCGrafPort[index]).portPixMap).baseAddr);
  214.             CloseCPort(myCGrafPtr[index]);
  215.             myCGrafPtr[index] = 0L;            
  216.             gOffscreenNeedsUpdate[index]=TRUE;
  217.         }
  218.         
  219.         if (myCGrafPtr[index]==0L)
  220.         {
  221.             /* if we just switched from b/w to color, delete the b/w port */
  222.             if(myGrafPtr[index] != 0L)
  223.             {
  224.                 DisposePtr(myGrafPort[index].portBits.baseAddr);
  225.                 ClosePort(myGrafPtr[index]);
  226.                 myGrafPtr[index] = 0L;
  227.                 gOffscreenNeedsUpdate[index]=TRUE;
  228.             }
  229.             
  230.             if (gBiggestDevice)
  231.             {
  232.                 oldDevice = GetGDevice();
  233.                 SetGDevice(gBiggestDevice);
  234.             }
  235.             else
  236.                 oldDevice = 0L;
  237.             
  238.             myCGrafPtr[index] = &myCGrafPort[index];
  239.             OpenCPort(myCGrafPtr[index]);
  240.             gLastDepth[index] = theDepth = (**(myCGrafPort[index]).portPixMap).pixelSize;
  241.             if (theDepth>gMaxDepth[index])
  242.                 gLastDepth[index]=theDepth=gMaxDepth[index];
  243.             
  244.             offRowBytes = (((theDepth * (bRect[index].right - bRect[index].left)) + 15) >> 4) << 1;
  245.             sizeOfOff = (long)(bRect[index].bottom - bRect[index].top) * offRowBytes;
  246.             OffsetRect(&bRect[index], -bRect[index].left, -bRect[index].top);
  247.             
  248.             myBits[index] = NewPtr(sizeOfOff);
  249.             if(myBits[index] == 0L)
  250.             {
  251.                 CloseCPort(myCGrafPtr[index]);
  252.                 myCGrafPtr[index]=0L;
  253.                 ErrorString("\pTHERE 15 N0T ENUF MEM0RY T0 0PEN THE W1ND0W!!", "\p");
  254.             }
  255.             
  256.             (**(myCGrafPort[index]).portPixMap).baseAddr = myBits[index];
  257.             (**(myCGrafPort[index]).portPixMap).rowBytes = offRowBytes + 0x8000;
  258.             (**(myCGrafPort[index]).portPixMap).bounds = bRect[index];
  259.             
  260.             myCGrafPort[index].portRect = bRect[index];
  261.             
  262.             ourCMHandle[index] = (**(**gBiggestDevice).gdPMap).pmTable;
  263.             err = HandToHand(&ourCMHandle[index]);
  264.             if(err != noErr)
  265.             {
  266.                 DisposePtr((**(myCGrafPort[index]).portPixMap).baseAddr);
  267.                 CloseCPort(myCGrafPtr[index]);
  268.                 myCGrafPtr[index]=0L;
  269.                 ErrorString("\pTHERE 15 N0T ENUF MEM0RY T0 0PEN THE W1ND0W!!", "\p");
  270.             }
  271.             
  272.             for(i = 0; i <= (**ourCMHandle[index]).ctSize; i++)
  273.                 (**ourCMHandle[index]).ctTable[i].value = i;
  274.             (**ourCMHandle[index]).ctFlags &= 0x7fff;
  275.             (**ourCMHandle[index]).ctSeed = GetCTSeed();
  276.             
  277.             (**(myCGrafPort[index]).portPixMap).pmTable = ourCMHandle[index];
  278.             
  279.             if (oldDevice)
  280.                 SetGDevice(oldDevice);
  281.         }
  282.         
  283.         UpdateTheWindowColor(index);
  284.     }
  285.     else
  286.     {
  287.         if (myGrafPtr[index]==0L)
  288.         {
  289.             if(myCGrafPtr[index] != 0L)
  290.             {
  291.                 DisposeHandle((**(myCGrafPort[index]).portPixMap).pmTable);
  292.                 DisposePtr((**(myCGrafPort[index]).portPixMap).baseAddr);
  293.                 CloseCPort(myCGrafPtr[index]);
  294.                 myCGrafPtr[index] = 0L;
  295.                 gOffscreenNeedsUpdate[index]=TRUE;
  296.             }
  297.             
  298.             myGrafPtr[index] = &myGrafPort[index];
  299.             OpenPort(myGrafPtr[index]);
  300.             
  301.             offRowBytes = (((bRect[index].right - bRect[index].left) + 15) >> 4) << 1;
  302.             sizeOfOff = (long)(bRect[index].bottom - bRect[index].top) * offRowBytes;
  303.             OffsetRect(&bRect[index], -bRect[index].left, -bRect[index].top);
  304.             
  305.             myBits[index] = NewPtr(sizeOfOff);
  306.             if(myBits[index] == 0L)
  307.             {
  308.                 ClosePort(myGrafPtr[index]);
  309.                 myGrafPtr[index]=0L;
  310.                 ErrorString("\pTHERE 15 N0T ENUF MEM0RY T0 0PEN THE W1ND0W!!", "\p");
  311.             }
  312.             
  313.             myGrafPort[index].portBits.baseAddr = myBits[index];
  314.             myGrafPort[index].portBits.rowBytes = offRowBytes;
  315.             myGrafPort[index].portBits.bounds = bRect[index];
  316.             myGrafPort[index].portRect = bRect[index];            
  317.         }
  318.         
  319.         UpdateTheWindowBW(index);
  320.     }
  321.     
  322.     ValidRect(&(gTheWindow[index]->portRect));
  323. }
  324.  
  325. void UpdateTheWindowColor(int index)
  326. {
  327.     GDHandle    oldDevice;
  328.     RgnHandle    oldClipRgn;
  329.     RgnHandle    newClipRgn;
  330.     
  331.     if (gOffscreenNeedsUpdate[index])
  332.     {
  333.         oldDevice = GetGDevice();
  334.         SetGDevice(gBiggestDevice);
  335.         oldClipRgn = myCGrafPort[index].clipRgn;
  336.         newClipRgn=NewRgn();
  337.         SetRectRgn(newClipRgn, 0, 0, gWindowWidth[index], gWindowHeight[index]);
  338.         
  339.         myCGrafPort[index].clipRgn=newClipRgn;
  340.             
  341.         SetPort((GrafPtr)myCGrafPtr[index]);
  342.         
  343.         switch (index)
  344.         {
  345.             case kAbout:
  346.                 DrawTheAboutBox(TRUE);
  347.                 break;
  348.             case kAboutMSG:
  349.                 DrawTheCarpet();
  350.                 break;
  351.             case kHelp:
  352.                 DrawTheHelp(TRUE);
  353.                 break;
  354.         }
  355.         SetGDevice(oldDevice);
  356.         myCGrafPort[index].clipRgn = oldClipRgn;
  357.         DisposeRgn(newClipRgn);
  358.         gOffscreenNeedsUpdate[index]=FALSE;
  359.     }
  360.     
  361.     SetPort(gTheWindow[index]);
  362.     
  363.     CopyBits(&(((GrafPtr)myCGrafPtr[index])->portBits),
  364.                 &(gTheWindow[index]->portBits), &bRect[index], &bRect[index], 0, 0L);
  365. }
  366.  
  367. void UpdateTheWindowBW(int index)
  368. {
  369.     RgnHandle    oldClipRgn;
  370.     RgnHandle    newClipRgn;
  371.     
  372.     if (gOffscreenNeedsUpdate[index])
  373.     {
  374.         oldClipRgn = myGrafPort[index].clipRgn;
  375.         newClipRgn=NewRgn();
  376.         SetRectRgn(newClipRgn, 0, 0, gWindowWidth[index], gWindowHeight[index]);
  377.         myCGrafPort[index].clipRgn=newClipRgn;
  378.         SetPort(myGrafPtr[index]);
  379.         switch (index)
  380.         {
  381.             case kAbout:
  382.                 DrawTheAboutBox(FALSE);
  383.                 break;
  384.             case kAboutMSG:
  385.                 DrawTheCarpet();
  386.                 break;
  387.             case kHelp:
  388.                 DrawTheHelp(FALSE);
  389.                 break;
  390.         }
  391.         myGrafPort[index].clipRgn = oldClipRgn;
  392.         DisposeRgn(newClipRgn);
  393.         gOffscreenNeedsUpdate[index]=FALSE;
  394.     }
  395.     
  396.     SetPort(gTheWindow[index]);
  397.     
  398.     CopyBits(&(myGrafPtr[index]->portBits),
  399.                 &(gTheWindow[index]->portBits), &bRect[index], &bRect[index], 0, 0L);
  400. }
  401.  
  402. void UpdateHelpWindow(void)
  403. {
  404.     gOffscreenNeedsUpdate[kHelp]=TRUE;
  405.     OpenTheWindow(kHelp);
  406. }
  407.  
  408. void CloseTheWindow(int index)
  409. {
  410.     DisposeWindow(gTheWindow[index]);
  411.     gTheWindow[index]=0L;
  412.     
  413.     if (index==kHelp)
  414.         gOffscreenNeedsUpdate[kHelp]=TRUE;
  415. }
  416.  
  417. void ShutDownMSGGraphics(void)
  418. {
  419.     int                i;
  420.     
  421.     for (i=0; i<NUM_WINDOWS; i++)
  422.     {
  423.         if ((myCGrafPtr[i]!=0L) || (myGrafPtr[i]!=0L))
  424.             DisposPtr(myBits[i]);
  425.         if(myCGrafPtr[i] != 0L)
  426.         {
  427.             DisposeHandle((**(myCGrafPort[i]).portPixMap).pmTable);
  428.             CloseCPort(myCGrafPtr[i]);
  429.             myCGrafPtr[i] = 0L;
  430.         }
  431.         if(myGrafPtr[i] != 0L)
  432.         {
  433.             ClosePort(myGrafPtr[i]);
  434.             myGrafPtr[i] = 0L;
  435.         }
  436.     }
  437. }
  438.